package com.zhao.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhao.common.support.Response;
import com.zhao.common.support.StatusCodeEnum;

/**
 * ajax登录认证
 * 
 * @author zhaoyongfei
 *
 */
@Deprecated
@Controller
public class LoginController extends FrontBaseController {

	@Autowired
	AuthenticationManager authenticationManager;

	public String loginPage = "back/login";
	public String loginSuccessPage = "";
	public String loginFailurePage = "";

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String view(Model model) {
		return loginPage;
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(User user, Model model) {
		String errorMsg = "";
		String errorCode = "";
		if (user == null || user.getUsername() == null || user.getPassword() == null) {
		}
		Authentication token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
		try {
			Authentication result = authenticationManager.authenticate(token);
			SecurityContextHolder.getContext().setAuthentication(result);
			if (result.isAuthenticated()) {
				return loginSuccessPage;
			}
		} catch (BadCredentialsException e) {
			e.printStackTrace();

		} catch (AuthenticationException e) {
			e.printStackTrace();

		}
		return loginFailurePage;
	}

	/**
	 * 登入
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "login")
	public @ResponseBody Response login(@RequestBody Map<String, String> map, HttpServletRequest request) {
		if (map == null || map.get("username") == null || map.get("password") == null) {
			return Response.backResponse(StatusCodeEnum.EMPTY_PARAMETER, "参数不可为空");
		}
		String username = map.get("username");
		String password = map.get("password");
		Authentication token = new UsernamePasswordAuthenticationToken(username, password);
		try {
			Authentication result = authenticationManager.authenticate(token);
			SecurityContextHolder.getContext().setAuthentication(result);
			if (result.isAuthenticated()) {
				return Response.backSuccess(result);
			}
		} catch (BadCredentialsException e) {
			e.printStackTrace();
			return Response.backResponse(StatusCodeEnum.LOGIN_FAILTURE, "账户密码不匹配");
		} catch (AuthenticationException e) {
			e.printStackTrace();
			return Response.backResponse(StatusCodeEnum.LOGIN_FAILTURE, "服务器忙");
		}
		return Response.backResponse(StatusCodeEnum.LOGIN_FAILTURE, "登录失败");
	}

	/**
	 * 验证当前用户是否登录
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "login/auth")
	public @ResponseBody Response signAuth(HttpServletRequest request, HttpServletResponse response) {
		try {
			SecurityContext context = SecurityContextHolder.getContext();
			return Response.backSuccess(context.getAuthentication());
		} catch (Exception e) {
			e.printStackTrace();
			return Response.backResponse(StatusCodeEnum.SYSTEM_EXCEPTION, "登出失败");
		}
	}

	/**
	 * @param model
	 * @return
	 */
	/*
	 * @RequestMapping(value = "logout") public String logout(Model model) {
	 * 
	 * return loginPage; }
	 */
	/**
	 * 登出
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "logout")
	public @ResponseBody Response logout(HttpServletRequest request, HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(false);
			if (session != null) {
				session.invalidate();
			}

			SecurityContext context = SecurityContextHolder.getContext();
			context.setAuthentication(null);

			SecurityContextHolder.clearContext();
			return Response.backSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			return Response.backResponse(StatusCodeEnum.SYSTEM_EXCEPTION, "登出失败");
		}
	}
}
